package activitys;

import io.web.JSONConnector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import nl.preflag.basewars.R;
import org.json.JSONArray;
import org.json.JSONObject;
import misc.SpecialAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;

/**
 * @author Patrick van Gils, 0787862, INF3M
 * Ranking list
 */

public class RankListActivity extends Activity {

	private JSONConnector webby;
	private JSONArray rankListJson;
	public HashMap<String, String> map, o, rankMap;
	
	ImageButton rankMapButton;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.rank_list);

        setView();
        fillRankList();
    }
	
	/**
	 * Set the views
	 */
	private void setView() {
		rankMapButton = (ImageButton) findViewById(R.id.rankMapButton);
		
		rankMapButton.setOnClickListener(new View.OnClickListener() {
	          public void onClick(View v) {
	        	  openMapView();
	          }
		});
	}
	
	/** 
	 * Fill the ranking list
	 */
	private void fillRankList() {
		final ListView lv = (ListView) findViewById(R.id.rankListListView);
		String[] from = new String[] {"username", "credits", "image", "ranktext"};
		
		List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    	try 
    	{
    		webby = new JSONConnector();
    		rankListJson = webby.connect("/ranking", "result", false);

    		for(int i=0;i<rankListJson.length();i++) 
    		{
    			
    			map = new HashMap<String, String>();	
    			JSONObject e = rankListJson.getJSONObject(i);
    			
    			map.put("user_id",  ""+e.getInt("user_id"));
    			map.put("username", e.getString("username"));
    			map.put("credits", ""+e.getInt("credits"));
    			map.put("ranktext", ""+(i+1));
    			switch (i) {
    			case 0:
    				map.put("image", ""+R.drawable.rank_1);
    				break;
    			case 1:
    				map.put("image", ""+R.drawable.rank_2);
    				break;
    			case 2:
    				map.put("image", ""+R.drawable.rank_3);
    				break;
    			case 3:
    				map.put("image", ""+R.drawable.rank_4);
    				break;
    			case 4:
    				map.put("image", ""+R.drawable.rank_5);
    				break;
    			case 5:
    				map.put("image", ""+R.drawable.rank_6);
    				break;
    			case 6:
    				map.put("image", ""+R.drawable.rank_7);
    				break;
    			case 7:
    				map.put("image", ""+R.drawable.rank_8);
    				break;
    			case 8:
    				map.put("image", ""+R.drawable.rank_9);
    				break;
    			case 9:
    				map.put("image", ""+R.drawable.rank_10);
    				break;
    			default:
    				map.put("image", ""+R.drawable.rank_0);
    				break;
    			}
    			fillMaps.add(map);	
    		}
	     	
	   	} catch (Exception e) 
	   	{
	   		e.printStackTrace();
	   	}
    	// fill in the grid_item layout
        SpecialAdapter adapter = new SpecialAdapter(this, fillMaps, R.layout.rank_list_item, from, new int[] { R.id.rankListUsernameTextView, R.id.rankListScoreTextview, R.id.rankListRankIcon, R.id.rankListRankTextView});
        lv.setAdapter(adapter);
	}
	
	/**
	 * Open the map view
	 */
	private void openMapView() {
		finish();
	}
}
